{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca465d0d",
   "metadata": {},
   "source": [
    "## Z变换实现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6bfe17c",
   "metadata": {},
   "source": [
    "计算有限长序列的$z$变换$N_1<=n<=N_2$的$z$变换表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4948ec50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'-4*z^-3+3*z^-4+-5*z^-5+-2*z^-6+5*z^-7+5*z^-8+-2*z^-9+-1*z^-10+8*z^-11+-3*z^-12'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def chripz(N1,N2,X):\n",
    "    N = N1 - 1\n",
    "    text1 = '*z^'\n",
    "    text2 = '+'\n",
    "    for x in X:\n",
    "        N += 1\n",
    "        n = str(-N)\n",
    "        xx = str(x)\n",
    "        if N == N1:\n",
    "            Z = xx + text1 + n\n",
    "        else:\n",
    "            Z = Z + text2 + xx + text1 + n\n",
    "    return Z\n",
    "\n",
    "Z = chripz(3,13,np.array([-4,3,-5,-2,5,5,-2,-1,8,-3]))\n",
    "Z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b46bbaf6",
   "metadata": {},
   "source": [
    "## 逆Z变换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a4729c4",
   "metadata": {},
   "source": [
    "采用长除法求逆z变换，得出$x(n)$的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bb311643",
   "metadata": {},
   "outputs": [],
   "source": [
    "#libraries\n",
    "import numpy as np\n",
    "from math import *\n",
    "def longdiv(num,den,side,length):\n",
    "    #numerator为分子，denominator为分母\n",
    "    # side确定序列为左边序列还是右边序列（结合收敛域），length：想要输出的逆变换后x(n)系数的长度\n",
    "    L=np.arange(length)\n",
    "    if side =='left':\n",
    "        den=np.flipud(den)\n",
    "     #如果求|Z|<RX-收敛域内的Z逆变换，即为左边序列，则将分母的系数倒序计算,即升幂除法\n",
    "    if len(den)<len(num):\n",
    "        print('error z transform')#如果分子的z的阶次大于分母则有误\n",
    "    if len(num)!=len(den):\n",
    "        num=np.append(np.zeros([len(den)-len(num),1]),num)\n",
    "        res=[]\n",
    "        m=num\n",
    "    for i in L:\n",
    "        tempRes = m[0]/den[0]#长除\n",
    "        m=m-tempRes*den\n",
    "        m=np.append(m[1:len(m)],0)\n",
    "        res=np.append(res,tempRes)\n",
    "    return res\n",
    "#数组最右端为z的零次的系数，依次往左为更高阶次z次幂的系数\n",
    "#X(Z)=Z/(3Z^2-4Z+1)\n",
    "res_right = longdiv(np.array([1,0]),np.array([3,-4,1]),'right',6)\n",
    "res_left = longdiv(np.array([1,0]),np.array([3,-4,1]),'left',6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "91c2dacc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.33333333, 0.44444444, 0.48148148, 0.49382716,\n",
       "       0.49794239])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "38edd816",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.,   1.,   4.,  13.,  40., 121.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res_left"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
